<?php
/*
** @introduction: 处理会员申请分销
** @author: 杨陈鹏
** @email: yangchenpeng@cdlinglu.com
*/

namespace Admin\Controller\Member;


use Common\Controller\Admin\CommonController;

class DistributionController extends CommonController
{
    /*
    ** @introduction: 全部列表
    ** @author: 杨陈鹏
    ** @email: yangchenpeng@cdlinglu.com
    */
    public function lists()
    {

        $this->status           = I('param.status');

        $logic                  = D('Common/Member','Logic');

        $this->page             = I('param.page',1);

        $this->nums             = I('param.nums',20);

        $this->status           = I('param.status',false);


        $where                  = [];

        if($this->status)
            $where['status']    = $this->status;

        $data                   = $logic::distributionList($where,$this->page,$this->nums,'*');

        $this->data             = $data['data'];

        $this->total            = $data['total'];

        $this->display();

    }
    /*
    ** @introduction: 拒绝申请
    ** @author: 杨陈鹏
    ** @email: yangchenpeng@cdlinglu.com
    */
    public function refuse()
    {

        $data           = I('post.data');

        $logic          = D('Common/Member','Logic');

        $id             = $data['id'];
        $uid            = $data['uid'];
        $mobile         = $data['mobile'];

        unset($data['id']);
        unset($data['uid']);
        unset($data['mobile']);

        $data['status'] = 3;

        $result         = $logic::$apply_dis
                            ->where(['id' => $id])
                            ->save($data);

        if($result !== false)
        {

            $logic::$model->where(['uid' => $uid])->save(['micro_distribution' => 3]);

            $sms = new \Think\SMS\Mobile();

            $msg = "您申请的微分销被拒绝,拒绝理由:".$data['message'];

            $sms->send($mobile,$msg);

            $this->ajaxReturn(['code' => 200, 'msg' => '操作成功']);

        }

        $this->ajaxReturn(['code' => 300, 'msg' => '操作失败']);


    }
    /*
    ** @introduction: 同意申请
    ** @author: 杨陈鹏
    ** @email: yangchenpeng@cdlinglu.com
    */
    public function agree()
    {
        $data           = I('post.data');

        $logic          = D('Common/Member','Logic');

        $id             = $data['id'];
        $uid            = $data['uid'];
        $mobile         = $data['mobile'];
        if(floatval($data['commission_rate']) < 0 || floatval($data['commission_rate']) > 100)
            $this->ajaxReturn(['请输入正确的分成比例!']);
        unset($data['id']);
        unset($data['uid']);
        unset($data['mobile']);
        $data['status'] = 2;

        $result         = $logic::$apply_dis
                            ->where(['id' => $id])
                            ->save($data);

        if($result !== false)
        {

            $logic::$model->where(['uid' => $uid])->save(['micro_distribution' => 9]);

            $sms = new \Think\SMS\Mobile();

            $msg = "您申请的微分销已通过!";

            $sms->send($mobile,$msg);

            $this->ajaxReturn(['code' => 200, 'msg' => '操作成功']);

        }

        $this->ajaxReturn(['code' => 300, 'msg' => '操作失败']);

    }
}